//
//  TransactionRecordTableVC.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/27.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "TransactionRecordTableVC.h"
#import "TransactionRecordHeader.h"
#import "TransactionRecordCell.h"
#import "TransactionDetailTableVC.h"
#import "TransactionModel.h"
#import "DZHeadOption.h"
@interface TransactionRecordTableVC () <UITableViewDelegate,UITableViewDataSource,DZHeadOptionDelegate>
{
    NSInteger financialStyle;
    NSInteger _page;
    UIView *backView;
}
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataArray;
@end

@implementation TransactionRecordTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self leftButton:nil];
    [self setupTitleView:@"资金记录"];
    [self setHead];
    [self setTableView];
    financialStyle = 2;
    _page = 1;
    _dataArray = [NSMutableArray array];
    [self getInformation];
}

- (void)getInformation {
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"Order/getTransactionRecordList";
    request.params = @{@"order_type":@(financialStyle),
                       @"p":@(_page),
                       @"pagesize":@10};
    [DZNetAPI request:request success:^(id response) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        NSLog(@"%@",response);
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 1) {
            if (_page == 1) {
                [_dataArray removeAllObjects];
            }
            NSArray *array = model.data[@"list"];
            for (NSDictionary *dic in array) {
                [_dataArray addObject:[TransactionModel mj_objectWithKeyValues:dic]];
            }
            if (_dataArray.count == [model.data[@"count"] integerValue]) {
                [_tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        }else{
            JSSHOW(model.msg);
        }
        [self buildNoDataViewWithStatus:0];
    } failure:^(NSError *error) {
        JSSHOW(LOAD_FAIL_TIP);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)setHead {
    DZHeadOption *headView = [[DZHeadOption alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 44)];
    headView.delegate = self;
    headView.titleArray = @[@"充值",@"提现"];
    [self.view addSubview:headView];
}

- (void)setTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,108, SCREEN_WIDTH, SCREEN_HEIGHT - 44) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundView = nil;
    _tableView.backgroundColor = BACKGROUND_COLOR;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:TransactionRecordCells bundle:nil] forCellReuseIdentifier:TransactionRecordCells];
    _tableView.mj_header = [MJDIYHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self getInformation];
    }];
    _tableView.mj_footer = [MJDIYBackFooter footerWithRefreshingBlock:^{
        _page ++;
        [self getInformation];
    }];
}

- (void)DZHeadOptionView:(DZHeadOption *)DZHeadOption currectIndex:(NSInteger)currectIndex {
    [_tableView.mj_header beginRefreshing];
    if (currectIndex == 0) {
        financialStyle = 2;
    }else{
        financialStyle = 4;
    }
    [self getInformation];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TransactionRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:TransactionRecordCells];
    TransactionModel *model = _dataArray[indexPath.section];
    cell.financeLab.text = model.transaction_type;
    if (![model.transaction_time isKindOfClass:[NSNull class]]) {
        cell.investTime.text = model.transaction_time;
    }
    
    if (financialStyle == 2) {
        float money = [DZNsstringUtil floatInto:[model.transaction_amount doubleValue]];
        cell.investMoney.text = [NSString stringWithFormat:@"+%.2lf",money];
        cell.investMoney.textColor = RGB(254, 82, 50);
    }else if (financialStyle == 4){
        float money = [DZNsstringUtil floatInto:[model.transaction_amount doubleValue]];
        cell.investMoney.text = [NSString stringWithFormat:@"-%.2lf",money];
        cell.investMoney.textColor = RGB(55, 199, 108);
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TransactionModel *model = _dataArray[indexPath.section];
    TransactionDetailTableVC *transactionDetail = [[TransactionDetailTableVC alloc] init];
    transactionDetail.model = model;
    [self.navigationController pushViewController:transactionDetail animated:YES];
}

- (void)buildNoDataViewWithStatus:(NSInteger)status {
    if (_dataArray.count) {
        backView.hidden = YES;
        return;
    }else {
        if (backView) {
            backView.hidden = NO;
            return;
        }
    }
    status = 1;
    UIView *bkView = [[UIView alloc] initWithFrame:CGRectMake(0, (SCREEN_HEIGHT-SCREEN_WIDTH)/2.0, SCREEN_WIDTH, SCREEN_WIDTH)];
    bkView.backgroundColor = BACKGROUND_COLOR;
    UIImageView *img = [[UIImageView alloc] init];
    [bkView addSubview:img];
    UILabel *tipLab = [[UILabel alloc] init];
    tipLab.textAlignment = NSTextAlignmentCenter;
    tipLab.textColor = [UIColor lightGrayColor];
    tipLab.font = [UIFont systemFontOfSize:15];
    [bkView addSubview:tipLab];
    
    if (status == 1) {
        img.image = [UIImage imageNamed:@"空订单"];
        tipLab.text = @"暂时没有相关数据~";
    } else {
        img.image = [UIImage imageNamed:@"无网络"];
        tipLab.text = @"网络连接异常，请检查网络";
    }
    
    CGSize size = img.image.size;
    
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bkView.mas_centerX);
        make.width.mas_equalTo(size.width);
        make.height.mas_equalTo(size.height);
        make.top.equalTo(@12);
    }];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(bkView.mas_leading);
        make.trailing.equalTo(bkView.mas_trailing);
        make.top.equalTo(img.mas_bottom).offset(8);
        make.height.equalTo(@30);
    }];
    if (status == 0) {
        UIButton *reconnectBtn = [[UIButton alloc] init];
        [reconnectBtn setTitle:@"重新连接" forState:UIControlStateNormal];
        [reconnectBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        reconnectBtn.layer.masksToBounds = YES;
        reconnectBtn.layer.cornerRadius = BTN_RADIUS;
        reconnectBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        reconnectBtn.layer.borderWidth = 1;
        [reconnectBtn addTarget:self action:@selector(loadRequestData) forControlEvents:UIControlEventTouchUpInside];
        [bkView addSubview:reconnectBtn];
        
        [reconnectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(100, 30));
            make.centerX.equalTo(bkView.mas_centerX);
            make.top.equalTo(tipLab.mas_bottom).offset(8);
        }];
    }
    backView = bkView;
    [self.tableView addSubview:backView];
}

- (void)loadRequestData {
    [self.tableView.mj_header beginRefreshing];
}

@end
